/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Services;

import BackEnd.Linker;

import BackEnd.User;
import desktopapplication1.mainWin;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


/**
 *
 * @author Gabi
 */
public class Security {
    private static final String urlS="http://eiffel.itba.edu.ar/hci/service/Security.groovy";
    private static User actualUser = null;

    public static String getCharacterDataFromElement(Element e) {
	Node child = e.getFirstChild();

	if (child instanceof CharacterData) {
	   CharacterData cd = (CharacterData) child;
	   return cd.getData();
	}

	return "?";
  }


    public static User signIn(String user, String pass){

	User usr = null;

	if(user.length() < 8 || pass.length() < 8 || user.length() > 15 || pass.length() > 15 ){
	    System.out.println("Error en los datos ingresados");
	}


	String[] arg={"username="+user,"password="+pass };

	Document dom = null;

	try{
	    dom = Linker.getData(urlS,"SignIn", "GET", arg);
	}catch(Exception e){

	   return null;
	}

	NodeList list1 = dom.getElementsByTagName("token");
	NodeList list2 = dom.getElementsByTagName("user");

	Element tokenElem = (Element) list1.item(0);
	Element userElem = (Element) list2.item(0);

	String tokenAns = getCharacterDataFromElement(tokenElem);
	

	NamedNodeMap userAttributes = userElem.getAttributes();

	String idAns=userAttributes.getNamedItem("id").getNodeValue();
	String usernameAns=userAttributes.getNamedItem("username").getNodeValue();
	String nameAns=userAttributes.getNamedItem("name").getNodeValue();
	String lastLoginAns=userAttributes.getNamedItem("last_login_date").getNodeValue();

//	System.out.println(tokenAns);
//	System.out.println(idAns);
//	System.out.println(usernameAns);
//	System.out.println(nameAns);
//	System.out.println(lastLoginAns);

	usr = new User(usernameAns, nameAns, null, tokenAns, null, lastLoginAns, null, null);

        return usr;
    }

    public static int createAccount(String user, String name, String pass, String mail, String birthDay){
	
	try {
	    String[] xml = {"account=<account><username>" + user + "</username>" + "<name>" + name + "</name>" + "<password>" + pass + "</password>" + "<email>" + mail + "</email>" + "<birth_date>" + birthDay + "</birth_date></account>"};
	    Document rta = Linker.getData(urlS, "CreateAccount", "POST", xml);
	} catch (Exception ex) {
	    return 0;
	}


	return 1;
    }

    public static int signOut(String userName, String authToken) {
	    
	try {
	    String[] params = {"username="+userName,"authentication_token="+authToken};
	    Document rta = Linker.getData(urlS, "SignOut", "GET", params);
	} catch (Exception ex) {
	    return 0;
	}

	mainWin.user = null;

	return 1;
    }

    public static int changePassword(String userName, String actualPass, String newPass) {

	try {
	    String[] params = {"username="+userName,"password="+actualPass,"new_password="+newPass};
	    Document rta = Linker.getData(urlS, "ChangePassword", "GET", params);
	} catch (Exception ex) {
	    return 0;
	}

	return 1;
    }

    public static int getAccount(String userName, String authToken) {

	try {
	    String[] params = {"username="+userName,"authentication_token="+authToken};
	    Document rta = Linker.getData(urlS, "GetAccount", "GET", params);

	NodeList list1 = rta.getElementsByTagName("email");
	NodeList list2 = rta.getElementsByTagName("birth_date");
	NodeList list3 = rta.getElementsByTagName("created_date");

	Element mailElem = (Element) list1.item(0);
	Element birthDayElem = (Element) list2.item(0);
	Element creationDayElem = (Element) list3.item(0);

	String mailAns = getCharacterDataFromElement(mailElem);
	String birthDayAns = getCharacterDataFromElement(birthDayElem);
	String creationDayAns = getCharacterDataFromElement(creationDayElem);

	mainWin.getUser().setMail(mailAns);
	mainWin.getUser().setBirthDay(birthDayAns);
	mainWin.getUser().setCreationDate(creationDayAns);
	
	} catch (Exception ex) {
	    return 0;
	}

    return 1;
    }

    public static int updateAccount(String name, String mail, String birthDay) {

	try {
	    String[] xml = {"username="+mainWin.getUser().getUserName(),"authentication_token="+mainWin.getUser().getAuthToken(),"account=<account><name>" + name + "</name>" + "<email>" + mail + "</email>" + "<birth_date>" + birthDay + "</birth_date></account>"};
	    Document rta = Linker.getData(urlS, "UpdateAccount", "POST", xml);


	    mainWin.getUser().setName(name);
	    mainWin.getUser().setMail(mail);
	    mainWin.getUser().setBirthDay(birthDay);

	} catch (Exception ex) {
	    return 0;
	}

    return 1;
    }

  
}
